| PIZZA | Current compiler version: 0.39d |
| A substantial companion to Java | |
| Introduction to Java and Pizza | |
|
Contents
Home Mirrors FAQ Distribution
|
public boolean delete() {
SecurityManager security =
System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
return delete0();
}
/**
* a typical hello world! program.
*/
class HelloWorld {
/** the main method is executed by the command
*
* java HelloWord
*/
public static void main(String[] argv) {
System.out.println("hello world!");
}
}
java HelloWord
static public void main(String args[])
/usr/local/java/classes:/usr/local/lib/classes.zip
setenv CLASSPATH $HOME/classes:$CLASSPATH
alias jc javac -d ~/classes
alias pc java -ms8m pizza.compiler.Main -d ~/classes
If you use javac:
javac HelloWorld.java
or, if you define the alias:
jc HelloWord.java
If you want to use pizza instead, always make an alias, and use:
pc HelloWord.java
Finding out about options:
pc
==, !=, <, >, <=, >=, +, -, *, /, %, ++, --,
<<, >>, >>>, ~, &, |, ^
Format is IEEE 754
==, !=, <, >, <=, >=, +, -, *, /, %, ++, --
No floating point exceptions, overflows yield NaN.
byte < short
< int < long < float < double
char
long x = (long)2 // (long) redundant
byte x = (byte)257 // x == 1
b ? 1 : 0 and x != 0
class ASimpleClass {
int aVariable; // field
boolean aMethod() { // method
if (aVariable == 0) {
aVariable++;
return true;
} else {
return false;
}
}
ASimpleClass() { // constructor
aVariable = 0;
}
}
Accessing members of other objects:
ASimpleClass c = new ASimpleClass();
c.aMethod();
System.out.println(c.aVariable);
Accessing members of own object by unqualified name or using this:
Class Class1 {
int field;
boolean field2;
Class1(int field) {
this.field = field;
field2 = true;
}
}
class Class2 {
static int initialized = false;
static boolean init()
if (!initialized) {
initialised = true;
System.out.println("init done.");
}
}
int field;
Class2() { init(); }
}
Accessing static members of other classes:
Class2 obj = new Class2();
if (!Class2.initialized) obj.field = 1;
| Instance variables (one per object) | int x; |
| Static variables (one per class) | static int y; |
| Instance methods (one per object) | void incr_xy() { x ++; y++; } |
| Static methods (one per class) | static void incr_y() { y++; } |
| Constructor methods | C() { x = 0; } |
| Initializer blocks | { x = 0; } |
| Static initializer blocks | static { y = 0; } |
if (cond) S1
if (cond) S1 else S2
switch (sel) { case P1: S1
case Pn: Sn default: Sd }
while (cond) S
do S while (cond)
for (init; step; cond) S
L: S
break; break L;
continue; continue L;
return; return Expr;
throw Exception;
Missing:
goto L;
Are always generated dynamically, with new.
int[] intvec = new int[100];
double[][] matrix1 = new double[10][20];
double[][] matrix2 = new double[10][];
for (int I = 0; I < 10; I++) matrix2[I] = new double[20];
Access as in C:
intvec[99]
matrix[9]
matrix[9,19]
Additionally, one can find out about the length of an array
intvec.length;
matrix.length;
matrix[0].length;
Arrays are conceptually object types.
| Variable and array access: | x, a[I] |
| class- and instance-member access: | C.x, obj.x |
| Method calls: | m(x, y), o.m(x, y), C.m(x, y) |
| Object creation: | new C() |
| Arithmetic expressions: | |
| Assignments: | a = b; |
| Type-test: | obj instanceof C |
| Type-cast: | ( C ) obj, (int)1.0 |
| Conditional expression: | if (x > 0) 1 ? -1 |
group . project . class
company . library . class
pizza.compiler.Main
java.lang.System
| import java.io.File | lets you use File instead of java.io.File |
| import java.io.* | makes available all files in java.io in unqualified form. |
Always implicitly imported: java.lang
Organized as packages:
| java.lang | Basis classes: Object, String, System, Math |
| java.io | Input/Output |
| java.util | Utility classes: hash tables, vectors, enumerations |
| java.applet | Helper classes for applet execution |
| java.awt | Platform-independent graphical user interface |
| java.net | Network programming |
import java.io.*;
public class jcat {
private static final String usage =
"Usage: java jcat -v *";
/** copy file to standard output
*/
private static void copy(String filename) {
FileInputStream in;
try {
in = new FileInputStream(filename);
} catch (IOException ex) {
System.out.println(
"jcat: cannot open " + filename);
return;
}
try {
// read whole file in at once.
byte[] buf = new byte[in.available()];
in.read(buf);
// send whole buffer to standard output
System.out.write(buf, 0, buf.length);
in.close();
} catch (IOException ex) {
System.out.println("jcat: error on file " +
filename + ": " + ex);
}
}
public static void main(String[] args) {
int firstFileNameIndex = 0;
boolean verboseMode = false;
// check for proper usage
if (args.length == 0) {
System.err.println(usage);
return;
} else if (args[0].charAt(0) == '-') {
if (args[0].equals("-v")) && args.length > 1) {
firstFileNameIndex = 1;
verboseMode = true;
} else {
System.err.println(usage);
return;
}
}
// now process all files
for (int I = firstFileNameIndex; I < args.length;
I++) {
copy(args[I]);
if (verboseMode)
System.err.println(
"processed file " + args[I]);
}
}
}
java.lang.Integer
java.io.DataInputStream